require 'uri'  # ruby stdlib

module SitemapSpider

  class Sitemap
    private

    def get_url(from_url, str)
      url = URI.parse(str)
      if url.scheme
        url.scheme = url.scheme.downcase
        return nil unless url.scheme == @opts[:protocol]
      else
        url.scheme = @opts[:protocol]
      end
      if url.host
        url.host = url.host.downcase
        return nil unless url.host == @opts[:host]
        url.path = '/' if (url.path.nil? or url.path == '')
      else
        # if no host and no path, return nil (maybe return from_url.dup is more correct here, but not useful)
        return nil if (url.path.nil? or url.path == '')
        url.host = @opts[:host]
        # relative path (if the path does not begin with a slash)
        if not url.path.to_s =~ /\A\//
          url.path = without_string_after_last_slash(from_url.path) + url.path.to_s
        end
      end
      if url.port
        return nil unless url.port.to_s == @opts[:port.to_s]
      else
        url.port = @opts[:port]
      end
      normalize_port!(url)
      url.fragment = nil
      if @opts[:report_a_reference]
        url.referenced_from = from_url.to_s
        url.orig_href_string = str.dup
      end
      return url
      rescue URI::Error => err
        $stderr.printf("URI::Error:\na href :: %s\nfrom_url :: %s\nurl :: %s\nerror :: %s\n%s\n",
          str, from_url.inspect, url.inspect, err.inspect, err.backtrace.join("\n")) if @opts[:debug]
        $stderr.flush
        raise err if @opts[:errors_are_fatal]
        return nil
    end
    
    def normalize_port!(url)
      url.port = nil if ((url.port.to_s == '80' and url.scheme == 'http') or (url.port.to_s == '443' and url.scheme == 'https'))
      return url
    end
    
    def without_string_after_last_slash(str)
      nstr = str.dup
      if nstr =~ /\A(.*\/)[^\/]*\z/
        return $1
      end
      return nstr
    end
  end
end
